//
//  AppDelegate.m
//  gpos_app
//
//  Created by apple on 2018/8/13.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "AppDelegate.h"
#import "XMRegisterViewController.h"
#import "Reachability.h"
#import "MainViewController.h"
#import "LockController.h"
#import "PasswordCheckView.h"
#import "SVProgressHUD.h"
@interface AppDelegate ()
@property(nonatomic,strong)Reachability *reachability;
@end

@implementation AppDelegate
-(void)internetConnection
{   //网络连接现状
    NetworkStatus status =[self.reachability currentReachabilityStatus];
    switch (status) {
        case NotReachable:
            [SVProgressHUD showWithStatus:@"网络连接已断开"];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
            [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
            
            [[NSNotificationCenter defaultCenter]postNotificationName:@"netStatus" object:nil userInfo:@{@"netType":@"notReachable"}];
            
            break;
        case ReachableViaWiFi:
            
            NSLog(@"wifi连接");
            [[NSNotificationCenter defaultCenter]postNotificationName:@"netStatus" object:nil userInfo:@{@"netType":@"reachableViaWiFi"}];
            
            break;
        case ReachableViaWWAN:
            
            NSLog(@"移动蜂窝网络");
            [[NSNotificationCenter defaultCenter]postNotificationName:@"netStatus" object:nil userInfo:@{@"netType":@"reachableViaWWAN"}];
            
            break;
        default:
            break;
    }
    
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // Override point for customization after application launch.
    //监控网络状态
    //创建通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(internetConnection) name:kReachabilityChangedNotification object:nil];
    //初始化可达性
    self.reachability =[Reachability reachabilityForInternetConnection];
    //开始监听
    [self.reachability startNotifier];
    [self internetConnection];
    
   // 自动登录
//    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString * uname = [[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"];
    NSString * upwd = [[NSUserDefaults standardUserDefaults] objectForKey:@"passWord"];
    NSLog(@"zhanghao---%@",uname);
    NSLog(@"mima---%@",upwd);
    if (uname != NULL && upwd != NULL) {

        UINavigationController *_nav = (UINavigationController*) (self.window.rootViewController);
        MainViewController *_vc = [[MainViewController alloc] init];
        _nav.navigationBar.hidden=YES;
//        _vc.navigationController.navigationBar.hidden = YES;
        [_nav pushViewController:_vc animated:NO];
    
    }
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
//    UIViewController *vc=[(UINavigationController *)self.window.rootViewController visibleViewController];
    XMRegisterViewController *svc=[[XMRegisterViewController alloc] init];
    if ([[[self getCurrentVC] class] isEqual:[svc class]]){
        
        NSLog(@"登录");
        
    }else{
        
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:[[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"]] isEqualToString:@"YES"])
        {
            //设置了
            [[PasswordCheckView sharedInstance] show];
            NSLog(@"设置了");
        }else{
            
            //没有"
            NSLog(@"没有");
        }
        
    }
//    if ([svc isMemberOfClass:[vc class]]) {
//        //登录
//        NSLog(@"登录");
//    }else{
//
//        if ([[[NSUserDefaults standardUserDefaults] objectForKey:[[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"]] isEqualToString:@"YES"])
//        {
//           //设置了
//            [[PasswordCheckView sharedInstance] show];
//
//        }else{
//
//           //没有"
//            NSLog(@"没有");
//        }
//        //前台
//    }
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
   
   
    
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
//获取当前屏幕显示的viewcontroller

- (UIViewController *)getCurrentVC

{
    
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    
    UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
    
    
    
    return currentVC;
    
}
- (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC

{
    
    UIViewController *currentVC;
    
    if ([rootVC presentedViewController]) {
        
        // 视图是被presented出来的
    
        rootVC = [rootVC presentedViewController];
        
    }
    
    if ([rootVC isKindOfClass:[UITabBarController class]]) {
        
        // 根视图为UITabBarController
        
        currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
        
        
        
    } else if ([rootVC isKindOfClass:[UINavigationController class]]){
        
        // 根视图为UINavigationController
        
        
        currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
        
    } else {
        
        // 根视图为非导航类
    
        currentVC = rootVC;
        
    }
    return currentVC;
}

@end
